package cn.lyq.card.ui.bank

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.BankInfo
import cn.lyq.card.data.src.Status
import cn.lyq.card.global.BusTag
import cn.lyq.card.ui.common.KeyAdapter
import cn.lyq.card.utils.hasIndex
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_bank_select.*


class BankSelectActivity : BaseActivity() {

    private val mData = mutableListOf<BankInfo>()
    private val mDataTemp = mutableListOf<BankInfo>()
    private val mModel by lazy {
        getVM(BankVM::class.java)
    }
    private val mAdapter by lazy {
        KeyAdapter()
    }

    override fun getLayoutId(): Int {
        return R.layout.act_bank_select
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_bank_card_select)
        rv.layoutManager = LinearLayoutManager(this)
        rv.adapter = mAdapter
        mAdapter.setEmptyView(R.layout.layout_empty)
        mAdapter.isShowArrow = false
    }

    override fun initListener() {
        super.initListener()
        refreshLayout.setOnRefreshListener {
            getData()
        }
        tvSearch.setClick {
            KeyboardUtils.hideSoftInput(etSearch)
            getData()
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            if (mDataTemp.hasIndex(position)) {
                BusUtils.post(BusTag.BANK_SELECT, mDataTemp[position])
                finish()
            }
        }
    }

    private fun getData() {
        val key = etSearch.text.trim().toString()
        if (mData.size == 0){
            getFromService(key)
        }else{
            getFromLocal(key)
        }
    }

    private fun getFromLocal(key: String) {
        val adapterData = mutableListOf<String>()
        mDataTemp.clear()
        mData.forEach {
            val name = it.name
            if (name?.contains(key) == true){
                adapterData.add(name)
                mDataTemp.add(it)
            }
        }
        mAdapter.key = key
        mAdapter.setList(adapterData)
    }

    private fun getFromService(key: String) {
        mModel.getBankList(key).observe(this, Observer {
            when (it.status) {
                Status.SUCCESS -> {
                    refreshLayout.isRefreshing = false
                    mAdapter.key = key
                    setData(it.requireData())
                }
                Status.LOADING -> {
                    if (!refreshLayout.isRefreshing) {
                        refreshLayout.isRefreshing = true
                    }
                }
                Status.ERROR -> {
                    refreshLayout.isRefreshing = false
                    ToastUtils.showShort(it.message.getMsg())
                }
            }
        })
    }

    override fun initObserver() {
        super.initObserver()
        getData()
    }

    private fun setData(data: MutableList<BankInfo>) {
        mData.clear()
        mData.addAll(data)
        mDataTemp.addAll(data)
        val adapterData = mutableListOf<String>()
        data.forEach {
            adapterData.add(it.name ?: "")
        }
        mAdapter.setList(adapterData)
    }

}